<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Termination extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->is_logged_in();
        $this->load->model('termination/termination_model', 'termination_model');
        $this->load->model('member/member_model', 'member_model');
        $this->load->model('contract/contract_model', 'contract_model');
        $this->load->model('provider/provider_model');
        $this->load->model('category/category_model');
        $this->load->model('product/product_model');
    }

    public function index() {
    	$this->config->load('site', TRUE);
        $users = $this->termination_model->getSearch( array() );
        $this->_data['members'] = $users;
        $this->_data['full_name'] = "";
        $this->_data['gender_value'] = "";
        $this->_data['owner_id'] = "";
        $this->_data['gender'] = $this->config->item('gender', 'site');
        $this->_data['status'] = $this->config->item('termination_status', 'site');
        $this->_data['owners'] = $this->member_model->getOwner( );
        $this->load->admintemplate('list', $this->_data);
    }
    public function search() {
    	$this->config->load('form_validate', TRUE);
    	$this->config->load('site', TRUE);
    	$this->_data['status'] = $this->config->item('termination_status', 'site');
    	$params['full_name'] = $this->input->post('full_name');
    	$params['gender_value'] = $this->input->post('gender');
    	$params['owner'] = $this->input->post('owner');
    	$params['owner_id'] = $this->input->post('owner_id');
    	$members = $this->termination_model->getSearch($params, $sort = 'status', $order = 'asc');
    	$this->_data['members'] = $members;
    	$this->_data['gender'] = $this->config->item('gender', 'site');
        $this->_data['owners'] = $this->member_model->getOwner( );
        $this->_data['owner_id'] = $params['owner_id'];
        $this->_data['owner'] = $params['owner'];
        $this->_data['full_name'] = $params['full_name'];
        $this->_data['gender_value'] = $params['gender_value'];
    	$this->load->admintemplate('list', $this->_data);
    }
    public function add() {
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->form_validation->set_rules($this->config->item('termination', 'form_validate'));
        $categorys = $this->category_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['categorys'] = $categorys;
        $providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = array();
        foreach ($providers as $key => $value) {
        	$this->_data['providers'][$value->id] = $value;
        }
        $this->_data['status'] = $this->config->item('termination_status', 'site');
        $this->_data['members'] = $this->member_model->getAll( );
        if ($this->form_validation->run() == FALSE) {
            $this->load->admintemplate('add', $this->_data);
        } else {
            $params['member_id'] = $this->input->post('member_id', TRUE);
            $params['category_id'] = $this->input->post('category_id', TRUE);
            $params['provider_id'] = $this->input->post('provider_id', TRUE);
            $params['product_id'] = $this->input->post('product_id', TRUE);
            $params['contract_number'] = $this->input->post('contract_number', TRUE);
            $params['status'] = $this->input->post('status', TRUE);
            $this->termination_model->insert($params);
            redirect('termination');
        }
    }

    public function edit($id) {
        $termination = $this->termination_model->find_id($id);
        $this->_data['termination'] = $termination;
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->_data['members'] = $this->member_model->getAll( );
        $params['member_id'] = $this->_data['termination']->member_id;
        $this->_data['contracts'] = $this->contract_model->getSearch( $params );
        $this->_data['status'] = $this->config->item('termination_status', 'site');
        $this->form_validation->set_rules($this->config->item('termination', 'form_validate'));
        $categorys = $this->category_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['categorys'] = $categorys;
        $providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = $providers;
        $params['category_id'] = $termination->category_id;
        $params['provider_id'] = $termination->provider_id;
        $products = $this->product_model->getSearch($params);
        $this->_data['products'] = $products;
        if ($this->form_validation->run() == FALSE) {
            $this->load->admintemplate('edit', $this->_data);
        } else {
            $params['member_id'] = $this->input->post('member_id', TRUE);
            $params['category_id'] = $this->input->post('category_id', TRUE);
            $params['provider_id'] = $this->input->post('provider_id', TRUE);
            $params['product_id'] = $this->input->post('product_id', TRUE);
            $params['contract_number'] = $this->input->post('contract_number', TRUE);
            $params['status'] = $this->input->post('status', TRUE);
            $this->termination_model->update($id, $params);
            redirect('termination');
        }
    }
    public function email($id) {
    	$terminations = $this->termination_model->getForEmail($id);
    	$this->load->library('form_validation');
    	$this->config->load('form_validate', TRUE);
    	$this->form_validation->set_rules($this->config->item('terminationEmail', 'form_validate'));
    	$termination = $terminations[0];
    	$this->_data['termination'] = $termination;
    	$content = $termination->content_letter;
    	$content = str_replace('[CATEGORY_NAME]', $termination->category_name, $content);
    	$content = str_replace('[PROVIDER_NAME]', $termination->provider_name, $content);
    	$content = str_replace('[PRODUCT_NAME]', $termination->product_name, $content);
    	$content = str_replace('[CONTRACT_NUMBER]', $termination->contract_number, $content);
    	$content = str_replace('[USER_EMAIL]', $termination->email, $content);
    	$content = str_replace('[FIRST_NAME]', $termination->first_name, $content);
    	$content = str_replace('[LAST_NAME]', $termination->last_name, $content);
    	$content = str_replace('[ADDRESS]', $termination->address, $content);
    	$content = str_replace('[HOUSE_NUMBER]', $termination->house_number, $content);
    	$content = str_replace('[POSTCODE]', $termination->postcode, $content);
    	$content = str_replace('[PLACE]', $termination->place, $content);
    	$this->_data['content'] = $content;
    	if ($this->form_validation->run() == FALSE) {
    		$this->load->admintemplate('email', $this->_data);
    	} else {
    		//send email to user
    		$this->load->library( 'tcpdf/tcpdf' );
    		$pdf = new TCPDF("P", "mm", "A4", true);
    		$pdf->setPrintHeader(true);
    		$pdf->setPrintFooter(true);
    		$pdf->SetMargins(5, 28, 5, 5);
    		$pdf->AddPage();
    		$pdf->WriteHTML($content, true);
    		$pdf->Output("assets/uploads/pdf/" . $termination->id."_".$termination->first_name."_".$termination->last_name . ".pdf", "F");
    		$this->load->library( 'PHPMailer/phpmailer' );
    		$mail = new PHPMailer();
    $mail->IsSMTP(); //  SMTP
    $mail->SMTPDebug = 0;  // 
    $mail->SMTPAuth = true;  // 
    $mail->SMTPSecure = 'ssl'; // 
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465; 
    $mail->Username = "nguyenhoangvu29@gmail.com";  
    $mail->Password = "huynhvu123";  
    $from = "nguyenhoangvu29@gmail.com";
    $from_name = "Vu Nguyen";         
    $mail->SetFrom($from, $from_name);                                  // Set mailer to use SMTP
    		$mail->AddAddress('opensourcesolution@yahoo.com', 'Josh Adams');  // Add a recipient
    		//$mail->AddAddress('ellen@example.com');               // Name is optional
    		$mail->AddReplyTo('info@example.com', 'Information');
    		$mail->AddCC('nguyenhoangvu29@gmail.com');
    		//$mail->AddBCC('bcc@example.com');
    		
    		//$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
    		//$mail->AddAttachment('/var/tmp/file.tar.gz');         // Add attachments
    		//$mail->AddAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
    		$mail->IsHTML(true);                                  // Set email format to HTML
    		
    		$mail->Subject = 'Here is the subject';
    		$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    		//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    		
    		if(!$mail->Send()) {
    			echo 'Message could not be sent.';
    			echo 'Mailer Error: ' . $mail->ErrorInfo;
    			exit;
    		}
    		redirect('termination');
    	}
    }

    public function delete() {
        $id = $this->input->post('id');
        echo $this->termination_model->delete($id);
        die;
    }

}
